package com.hdu.clj.dncard.databaseUtil

import android.content.ContentValues
import android.content.Context
import android.util.Log
import com.hdu.clj.dncard.database.AllEquipDatabase
import com.hdu.clj.dncard.database.HeroDatabase
import com.hdu.clj.dncard.database.UserDatabase
import com.hdu.clj.dncard.util.Hero

class HeroDatabaseUtil {
    /**
     * "id integer primary key autoincrement," +
    "hid text," +
    "acc text," +
    "attack integer," +
    "defence integer," +
    "wisdom integer," +
    "life integer," +
    "equipment integer)"
     */
    fun initInsert(context: Context,ver:Int,user:String,hero:Hero){
        val dbHero = HeroDatabase(context,"Hero.db",ver,user)
        val dbh = dbHero.writableDatabase
        val values = ContentValues().apply {
            put("hid", hero.hid)
            put("acc", user)
            put("attack", hero.attack)
            put("defence",hero.defence)
            put("wisdom",hero.wisdom)
            put("life",hero.life)
            put("equipment",hero.equipment)
        }
        dbh.insert("Hero", null, values)
        val dbHelper = UserDatabase(context,"Users.db",1)
        val db = dbHelper.writableDatabase
        db.execSQL("update Users set isCreate = ? where acc = ?", arrayOf(1,user) )
    }

    //查找角色属性（基础属性+装备加成）
    fun search(context: Context,ver: Int,user: String):Array<Int>{
        var attack = 0
        var defence = 0
        var wisdom = 0
        var life = 0
        val dbHero = HeroDatabase(context,"Hero.db",ver,user)
        val dbh = dbHero.writableDatabase
        val find = dbh.rawQuery("select * from Hero where acc = ?", arrayOf(user))
        if (find.moveToFirst()){
            attack = find.getInt(find.getColumnIndex("attack"))
            defence = find.getInt(find.getColumnIndex("defence"))
            wisdom = find.getInt(find.getColumnIndex("wisdom"))
            life = find.getInt(find.getColumnIndex("life"))
        }
        val dbAll = AllEquipDatabase(context,"AllEquip",1)
        val dba = dbAll.writableDatabase
        val findall = dba.rawQuery("select * from AllEquip where acc = ?", arrayOf(user))
        var harmor = ""
        var hhelmet = ""
        var hboots = ""
        var hglove = ""
        var hweapon = ""
        if (findall.moveToFirst()){
            harmor = findall.getString(findall.getColumnIndex("armor"))
            hhelmet = findall.getString(findall.getColumnIndex("helmet"))
            hboots = findall.getString(findall.getColumnIndex("boots"))
            hglove = findall.getString(findall.getColumnIndex("glove"))
            hweapon = findall.getString(findall.getColumnIndex("weapon"))
        }
        val h_armor = search_equip(context,harmor)
        val h_boots = search_equip(context,hboots)
        val h_helmet = search_equip(context,hhelmet)
        val h_glove = search_equip(context,hglove)
        val h_weapon = search_equip(context,hweapon)

        attack = attack + h_armor[0]+h_boots[0]+h_helmet[0]+h_glove[0]+h_weapon[0]
        wisdom = wisdom + h_armor[1]+h_boots[1]+h_helmet[1]+h_glove[1]+h_weapon[1]
        life = life + h_armor[2]+h_boots[2]+h_helmet[2]+h_glove[2]+h_weapon[2]

        return arrayOf(attack,defence,wisdom,life,h_armor[0],h_armor[1],h_armor[2],h_helmet[0],h_helmet[1],h_helmet[2]
        ,h_boots[0],h_boots[1],h_boots[2],h_glove[0],h_glove[1],h_glove[2],h_weapon[0],h_weapon[1],h_weapon[2])
    }

    fun search_equip(context: Context,equip:String):Array<Int>{
        val equip_hero = EquipDatabaseUtil().efind(context,equip)
        return equip_hero
    }

    fun search_equipname(context: Context,user: String):Array<String>{
        val dbAll = AllEquipDatabase(context,"AllEquip",1)
        val dba = dbAll.writableDatabase
        val findall = dba.rawQuery("select * from AllEquip where acc = ?", arrayOf(user))
        var harmor = ""
        var hhelmet = ""
        var hboots = ""
        var hglove = ""
        var hweapon = ""
        if (findall.moveToFirst()){
            harmor = findall.getString(findall.getColumnIndex("armor"))
            hhelmet = findall.getString(findall.getColumnIndex("helmet"))
            hboots = findall.getString(findall.getColumnIndex("boots"))
            hglove = findall.getString(findall.getColumnIndex("glove"))
            hweapon = findall.getString(findall.getColumnIndex("weapon"))
        }
        val zb1 = EquipDatabaseUtil().efindname(context,harmor)
        val zb2 = EquipDatabaseUtil().efindname(context,hhelmet)
        val zb3 = EquipDatabaseUtil().efindname(context,hboots)
        val zb4 = EquipDatabaseUtil().efindname(context,hglove)
        val zb5 = EquipDatabaseUtil().efindname(context,hweapon)

        return arrayOf(zb1,zb2,zb3,zb4,zb5)

    }

    fun findHero(context: Context,acc:String):Int{
        val dbHero = HeroDatabase(context,"Hero.db",1,acc)
        val dbh = dbHero.writableDatabase
        val find = dbh.rawQuery("select * from Hero where acc = ?", arrayOf(acc))
        if (find.moveToFirst()){
            return find.getInt(find.getColumnIndex("hid"))
        }
        return 0
    }
}